package com.ktjiaoyu.utils.fastjson2;
import com.alibaba.fastjson2.filter.ValueFilter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Set;
/**
 * @return 13141
 * 2025/7/7
 * 09:42
 */
public class FastJson2DateValueFilter implements ValueFilter {
    private LinkedHashMap<String, String> dateMappings = null;

    public FastJson2DateValueFilter(LinkedHashMap<String, String> dateMappings) {
        this.dateMappings = dateMappings;
    }

    @Override
    public Object apply(Object o, String s, Object o1) {
        if (dateMappings == null) {
            return o1;
        }
        // 要判断处理数据类型，只处理 Date 类型
        if (o1 instanceof Date) {
            Set<String> keySet = dateMappings.keySet();
            for (String pattern : keySet) {
//                    循环所有的正则表达式，判断属性名书否符合正则要求
                if (s.matches(pattern)) {
                    // 符合要求的取出格式进行转换
                    String dateFormat = dateMappings.get(pattern);
                    // 返回对应格式
                    SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
                    return formatter.format((Date) o1);
                }
            }
        }
        return o1;
    }
}
